Ext.namespace("Dudley");
Ext.namespace("Dudley.lims");

Dudley.lims.samplemgr = function() {

    var loaded = false;
    var housingPanel = {};
    Ext.History.init();
    var tokenDelimiter = ':';

    Ext.lib.Event.resolveTextNode = Ext.isGecko ? function(node) {
        if (!node) {
            return;
        }
        var s = HTMLElement.prototype.toString.call(node);
        if (s == '[xpconnect wrapped native prototype]' || s == '[object XULElement]') {
            return;
        }
        return node.nodeType == 3 ? node.parentNode : node;
    } : function(node) {
        return node && node.nodeType == 3 ? node.parentNode : node;
    };
       
    // public
    return {
        renderAdvancedSearch: function(datasource){
            Ext.Msg.alert("Info", "Under Development");    
        },
        loadSearchResult: function() {
            Ext.getDom("sample-search-result-list").innerHTML = "";
        },

        getSMLoaded: function() {
            return loaded;
        },

        getHousingPanel: function() {
            return housingPanel;
        },
        
        loadSampleManagement: function(activeSMResourceTab) {
            Ext.Msg.minWidth = 200;
            var win = new Ext.Window({
                title: 'Strains Loading...',
                width: 400,
                minWidth: 350,
                height: 150,
                modal: true,
                closeAction: 'hide',
                bodyStyle: 'padding:10px;',
                html: "Initializing Dudley Samples...",
                bbar: new Ext.ux.StatusBar({
                    id: 'init-yo-statusbar',
                    defaultText: 'Ready'
                })
            });

            win.on("show", function () {
                var sb = Ext.getCmp("init-yo-statusbar");
                sb.showBusy();
            });
            if (Dudley.lims.YOStrain.getYOStore() == null) {
                win.show();
            }
            Dudley.lims.YADStrain.initYAD(function () {
                Ext.Msg.alert("warning", "You do not have permission to view any Dudley strains");
                win.close();
            });
            Dudley.lims.KruglyakCrossing.initKruglyak(function () {
                Ext.Msg.alert("warning", "You do not have permission to view any Kruglyak Crossings");
                win.close();
            });
            Dudley.lims.Crossing.initCrossing(function () {
                Ext.Msg.alert("warning", "You do not have permission to view any Crossings");
                win.close();
            });
            Dudley.lims.YPGStrain.initYPG(function () {
                Ext.Msg.alert("warning", "Error loading Progeny Strains");
                win.close();
            });
            Dudley.lims.DeletionOligo.initDO(function () {
                Ext.Msg.alert("warning", "You do not have permission to view any Deletion Oligos");
                win.close();
            });
            Dudley.lims.LabOligo.initAO(function () {
                Ext.Msg.alert("warning", "You do not have permission to view any Lab Oligos");
                win.close();
            });
            Dudley.lims.PlasmidStrain.initPlasmid(function () {
                Ext.Msg.alert("warning", "You do not have permission to view any plasmid records");
                win.close();
            });
            Dudley.lims.Chemical.initChemical(function () {
                Ext.Msg.alert("warning", "You do not have permission to view any chemical records");
                win.close();
            });
            if (!loaded) {            
            Dudley.lims.YOStrain.initYO(function() {
                loaded = true;
                housingPanel = new Ext.TabPanel({
                    autoHeight: true,
                    activeTab: activeSMResourceTab,
                    border: true,
                    renderTo: "content-samples",
                    items: [ Dudley.lims.YOStrain.getYOPanel(), Dudley.lims.YADStrain.getYADPanel(),
                        Dudley.lims.Crossing.getCrossingPanel(), Dudley.lims.KruglyakCrossing.getKruglyakPanel(),
                        Dudley.lims.YPGStrain.getYPGPanel(), Dudley.lims.DeletionOligo.getDOPanel(), Dudley.lims.LabOligo.getAOPanel(),
                        Dudley.lims.PlasmidStrain.getPlasmidPanel(), Dudley.lims.Chemical.getChemicalPanel()],
                    listeners: {
                        'tabchange': function(tabPanel, tab) {
                            Ext.History.add(tabPanel.id + tokenDelimiter + tab.id);
                            win.close();
                        }
                    }
                });

            });
            }


        }
    };
}();
